package company.baidu;

import java.util.Arrays;

/**
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-09-11 20:15
 */
public class B02 {

    public static void main(String[] args) {
        String str = "cabcac";
        int[][] range = new int[][]{{1,6},{1,3},{2,5}};
        char[] chars = new B02().rangeFrequent(str, 3, range);
        System.out.println(String.valueOf(chars));
    }

    public char[] rangeFrequent(String str,int pair,int[][] rangeLR){
        char[] res = new char[pair];
        char[] c = str.toCharArray();
        for (int i = 0; i < pair; i++) {
            res[i] = helper(c,rangeLR[i][0]-1,rangeLR[i][1]-1);
        }

        return res;
    }

    public char helper(char[] c,int left,int right){
        int[] map  = new int[255];
        int max = 0;
        for (int i = left; i <= right; i++) {
            map[c[i]]++;
            if (map[c[i]] > max) max = map[c[i]];
        }

        char[] res  = new char[255];
        int index = 0;
        for (int i = 0; i < 255; i++) {
            if (map[i] == max) res[index++] = (char)i ;
        }

        Arrays.sort(res);
        for (int i = 0; i < 255; i++) {
            if (res[i] != 0) return res[i];
        }

        return 0;
    }
}
